global without sharing class RemoteActionForGecodeAdrress {
    public RemoteActionForGecodeAdrress(ApexPages.StandardController con){
        
    }
    @RemoteAction
    global static String gecodeAdress(String id,String coor,String type){
    ///id 工单ID  coor 经纬度
        System.debug(id  + coor);
        RemoteActionForGecodeAdrress.gecode(id,coor,type);
        return '1';
    }
    @RemoteAction
    global static String uploadAttach(String id,String attStr,String attName){

        try {
            Integer indx = attStr.indexOf(',');
            if(indx != -1 && attStr.length() > indx +1){

             Attachment att = new Attachment();
              att.Body = EncodingUtil.base64Decode(attStr.substring(indx+1) );
              att.ParentId = Id;
              att.Name = attName;

              insert att;
              return 'ok'; 
            }
             return '附件解析错误!';
        }catch (Exception e) {
            return e.getMessage();
        }
      

    }
     @future(callout=true)
    global static void gecode(String id,String coor,String type){
        
       // oce9KGavQcGiw2OfmIsELuQZLeVgcT4C    
        String url = 'http://api.map.baidu.com/geocoder/v2/?location='+coor+'&output=json&pois=1&ak=94040b618819150ffb2392c84e269f00';
       HttpRequest req = new HttpRequest();
            req.setMethod('GET');
            req.setHeader('Content-Type','text/plain');
            req.setEndpoint(url);

            System.debug('***'+url);
            HttpResponse res = Utils.postRequest(req, 3);
             //如果是测试
                if(Test.isRunningTest()){
                    res = new HttpResponse();
                    res.setHeader('Content-Type','application/x-www-form-urlencoded ; charset=GB2312');
                    res.setBody('{"status":0,"result":{"location":{"lng":103.11099999999996,"lat":30.11100018784859},"formatted_address":"四川省雅安市名山区"}}');
                    res.setStatusCode(200);
                }

  
            if (res.getStatusCode() == 200) {
                System.debug(id);
                System.debug(res.getBody());
                Map<String,Object> r = (Map<String,Object> ) JSON.deserializeUntyped(res.getBody());
               if(r.get('status')==0){
              		 Map<String,Object> rs = (Map<String,Object>) r.get('result');
                   	
               		WorkOrder__c wo = [select Id,StartOffAddress__c ,ArriveAddress__c from WorkOrder__c where Id =: Id];
              	 	if(type=='leave')wo.StartOffAddress__c = (String)rs.get('formatted_address');
               		if(type=='arrive')wo.ArriveAddress__c = (String)rs.get('formatted_address');
               		update wo;
               
               }

            }
        
    
    }
    

    
}